<template>
  <div class="login">
    <div class="logo">
      <img
        src="https://assets.maizuo.com/h5/mz-auth/public/app/img/logo.19ca0be.png"
      />
    </div>
    <div class="login-form">
      <form>
        <div class="form-group">
          <input
            type="tel"
            maxlength="11"
            placeholder="手机号"
            class="input-control"
            v-model="phone"
            @input="handleChange"
          />
          <div class="clear-mobile-btn" v-show="btnClear" @click="handleClear">
            <img src="@/assets/images/clear@1x.png" alt="">
          </div>
          <div :class="['getSmsCode sms-code', {'disable': getSmsCodeDisabled}]" @click='sendCaptcha'>
            <span v-show="!showTime">获取验证码</span>
            <span v-show="showTime">{{count}} s</span>
          </div>
        </div>
        <div class="errorTip" v-show="msg.phone">{{ msg.phone }}</div>
        <div class="form-group">
          <input placeholder="验证码" class="input-control" maxlength="6" v-model="smsCode" @input="handleChange" />
        </div>
        <div class="errorTip" v-show="msg.smsCode">{{ msg.smsCode }}</div>
        <div class="submit login-btn" @click="handleLogin"><span :class="{'activeDisable': loading}">登录</span></div>
      </form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      btnClear: false,
      phone: '', // 手机号
      smsCode: '', // 验证码
      msg: {
        phone: '',
        smsCode: ''
      },
      getSmsCodeDisabled: true,
      timer: null,
      count: 0,
      showTime: false,
      loading: true
    }
  },
  methods: {
    handleClear () {
      this.phone = ''
      this.btnClear = false
      if (this.phoneTip) {
        this.phoneTip = ''
        this.getSmsCodeDisabled = false
      }
    },
    handleChange () {
      if (this.phone.length !== 0) {
        if (!this.btnClear) this.btnClear = true
        if (this.msg.phone) this.msg.phone = ''
      }
      if (this.smsCode.length !== 0) {
        if (this.msg.smsCode) this.msg.smsCode = ''
      }
      const reg = /^1[0-9]{10}$/
      if (reg.test(this.phone)) {
        this.getSmsCodeDisabled = false
      } else {
        this.getSmsCodeDisabled = true
      }

      if (reg.test(this.phone) && /\d{6}/.test(this.smsCode)) {
        this.loading = false
      } else {
        this.loading = true
      }
    },
    // 验证手机号
    validMobile () {
      const reg = /^1[0-9]{10}$/
      if (reg.test(this.phone)) {
        return true
      } else {
        this.msg.phone = '请输入正确的手机号'
        return false
      }
    },
    validCaptcha () {
      var reg = /\d{6}/
      if (reg.test(this.smsCode)) {
        return true
      } else {
        this.msg.code = '请输入正确的验证码'
        return false
      }
    },
    handleLogin () {
      if (this.loading) return
      console.log('验证成功')
      this.$router.push({ path: '/center' })

      // this.loading = true
      // var data = this.loginForm
      // var phonePre = this.value.replace(/^\+/, '')
      // data['phonePre'] = phonePre
      // this.$store
      //   .dispatch('Login', data)
      //   .then((res) => { // 业务状态码(0,成功；1：选择列表；2：去认证)
      //     if (res) {
      //       if (res.status_code === 1) {
      //         // console.log('登录成功后，进入select')
      //         this.$router.push({ path: '/select' })
      //       } else {
      //         this.$router.replace({ path: '/' })
      //       }
      //     }
      //   })
      //   .catch((error) => {
      //     console.log(error)
      //     setTimeout(() => {
      //       this.loading = false
      //       this.showTime = true
      //       clearInterval(this.timer) // 清除定时器
      //       this.timer = null
      //     }, 1000)
      //   })
    },
    sendCaptcha () { // 发送验证码
      if (this.getSmsCodeDisabled) return
      // var phonePre = this.value.replace(/^\+/, '')
      if (this.validMobile()) {
        const TIME_COUNT = 60 // 更改倒计时时间
        if (!this.timer) {
          this.count = TIME_COUNT
          this.getSmsCodeDisabled = true
          this.showTime = true
          this.timer = setInterval(() => {
            if (this.count > 0 && this.count <= TIME_COUNT) {
              this.count--
            } else {
              this.showTime = false
              clearInterval(this.timer) // 清除定时器
              this.timer = null
              this.getSmsCodeDisabled = false
            }
          }, 1000)

          // sendCode(this.loginForm.phone, phonePre)
          //   .then(res => {
          //     // console.log(res);
          //   })
          //   .catch((error) => {
          //     console.log(error)
          //     this.showTime = true
          //     clearInterval(this.timer) // 清除定时器
          //     this.timer = null
          //   })
        }
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.login {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;right: 0;bottom: 0;left:0;
  background: #fff;
}
.logo {
  margin: 0.8rem auto 0.4rem;
  text-align: center;
  height: 0.6rem;
  img {
    width: 0.6rem;
    height: 100%;
  }
}
.errorTip {
  margin: 0 .24rem;
  line-height: 2em;
  font-size: .15rem;
  color: #ff5f16;
}
.form-group {
  position: relative;
  margin: 0 .24rem;
  height: .56rem;
  .input-control {
    height: .56rem;
    width: 100%;
    font-size: .15rem;
    color: #191a1b;
    border: 0;
    outline-width: 0;
  }
  &:after {
    content: " ";
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    color: #ededed;
    transform-origin: 0 0;
    border-bottom: 1px solid #ededed;
    transform: scaleY(.5);
  }
}
.getSmsCode {
  position: absolute;
  top: .16rem;
  right: 0;
  font-size: .14rem;
  min-width: .6rem;
  text-align: center;
}
.disable {
  color: #bdc0c5;
}
.login-form {
  .submit {
    margin: .7rem .25rem 0;
    height: .46rem;
    line-height: .46rem;
    font-size: .16rem;
    text-align: center;
    color: #fff;
    background-color: #ff5f16;
    border-radius: .03rem;
    border: none;
  }
  .activeDisable {
    opacity: .3;
  }
}
.clear-mobile-btn {
  position: absolute;
  right: .9rem;
  top: .14rem;
  width: .14rem;
  height: .14rem;
  z-index: 1;
  img {
    width: 100%;
    height: 100%;
  }
}
</style>
